// pages/visitors/visitors.js
var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dialog5: {
      hidden: true
    },
    upload: { hidden: true },

  },

  developer :{
    openid:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    /*wx.request({
      url: 'https://wwl.ycws.cc/ssm/v1/wwluserinfo',
      data: {
        starttime: "2010-01-01 00:00:00",
        idnumber:"a0c9cc92a2468159"
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);
      }
    })*/
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /*
  *访客
  */
  reserveHandle:function(){
    console.log("访客");
    wx.request({
      url: 'https://wwl.ycws.cc/ssm/visitors/openid',
      data: {
        openid: app.globalData.openId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data != null) {
          console.log("已经注册");
          var an = res.data;
          var json = JSON.stringify(an);
          app.owner.json = json;
          wx.redirectTo({
            url: '../../../pages/Visitor/showuser/showuser'
          })
        }else{
          wx.redirectTo({
            url: '../../../pages/Visitor/addvisitors/addvisitors'
          })
        }
      }
    })
  },
  /*
  *业主
  */
  owner: function () {
    console.log("业主");
    wx.navigateTo({
      url: '../../../pages/index/index'
    })
  },
  uploadChange: function (e) {
    this.setData({
      upload: {
        hidden: true
      }
    });
  },
  /**
   * 提交cook到后台查询openid，返回类型进行跳转
   */
  Jumpaa1: function (e) { //业主入口调用
    var that = this;
    that.setData({ 'dialog5.hidden': false });
    wx.login({
      success: function (res) {
        var cookma01 = res.code  //获取cook,时间是有效的
        wx.request({      //仿造AJAX调用
          url: "https://wwl.ycws.cc/wxpay/SelectOpenId/SelectOpenIdfin",
          //url: "http://localhost:9090/wxpay/SelectOpenId/SelectOpenIdfin", 
          method: "POST",
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: { cookema: cookma01, Serial: "1" },
          success: function (data) {
            that.setData({ 'dialog5.hidden': true });
            console.log(data.data.number);
            console.log(data.data.cookma);  
            wx.setStorageSync('developer', data.data.cookma);
            if (data.data.number == "2") {//2-数据库没有openid,能获取openid
              wx.redirectTo({
                url: "../../../pages/index/index",
              })
            } else if (data.data.number == "1"){//1-数据库有openid,能获取openid 

            console.log(data.data.ynumber)
              wx.setStorageSync('yphonetwo', data.data.yphone);
              wx.setStorageSync('developer', data.data.cookma);
              wx.setStorageSync("ynumbertwo", data.data.ynumber)
              var ynumber = wx.getStorageSync("ynumbertwo");//查询已经绑定的ID号

              console.log(ynumber)
              wx.request({      //仿造AJAX调用
                url: "https://wwl.ycws.cc/wxpay/SelectOpenId/Selectuploadfile",
                //url: "http://localhost:9090/wxpay/SelectOpenId/Selectuploadfile",
                method: "POST",
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data: { ynumber: ynumber },
                success: function (data) {
                  
                  if (data.data.number=="2"){//表示业主表中数据空没有
                    var phoneto = wx.getStorageSync("yphonetwo");
                    wx.navigateTo({
                      url: '../../adduser/adduser?phoneto='+ phoneto
                    })
                  }else{
                    
                    var ynumbertwo2=data.data.ynumber;
                    wx.setStorageSync("ynumbertwo", ynumbertwo2)
                    var ynumbertwo = wx.getStorageSync("ynumbertwo");
                    wx.navigateTo({
                      url: '../../newshowuser/newshowuser?ynumber=' + ynumbertwo
                    })
                    
                        console.log("跳转到业主展示")
                  }
                 },
                  fail: function () {//连不上服务，数据查询异常给予提示
                    that.setData({ 'dialog5.hidden': true });
                    that.setData({
                      upload: {
                        hidden: false
                      }
                    });
                    console.log("连接不上服务器")
                  }
              })

            }else {//获取不到open，出现异常
              console.log("出现异常")
            }
            
          },
          fail: function () {
            that.setData({ 'dialog5.hidden': true });
            that.setData({
              upload: {
                hidden: false
              }
            });
            console.log("连接不上服务器")
          },
        })
      }
    })
  },

    Jumpaa2: function () {
      var that = this;
      that.setData({ 'dialog5.hidden': false });
    wx.login({
      success: function (res) {
        var cookma01 = res.code  //获取cook,时间是有效的

        wx.request({      //仿造AJAX调用
          url: "https://wwl.ycws.cc/wxpay/fkSelectOpenId/fkSelectOpenIdfin",
         // url: "http://localhost:9090/wxpay/fkSelectOpenId/fkSelectOpenIdfin", 
          method: "POST",
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: { cookema: cookma01, Serial:"2"},
          success: function (data) {  
            that.setData({ 'dialog5.hidden': true });
            wx.setStorageSync('developer', data.data.cookma)
            console.log(wx.getStorageSync('developer'))
            if (data.data.number == "2") {//2-数据库没有openid,
            console.log("用户没有注册过")
             wx.redirectTo({
               url: "../../../pages/Visitor/addvisitors/addvisitors",
                          })
            } else if (data.data.number == "1"){//数据库有openid,已经进行了绑定

              console.log("数据库有openid,已经进行了绑定")
              wx.request({
                //url: 'http://localhost:8080/ssm/visitors/openid',
                url: 'https://wwl.ycws.cc/ssm/visitors/openid',
                method: "POST",
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data: {
                  openid: wx.getStorageSync('developer')
                },
                /*header: {
                  'content-type': 'application/json'
                },*/
                success: function (res) {
                  if (res.data==undefined){
                    that.setData({ 'dialog5.hidden': true });
                    that.setData({
                      upload: {
                        hidden: false
                      }
                    });
                  
                       }else{
                    var an = res.data;
                    var json = JSON.stringify(an);
                    app.owner.json = json;
                    wx.navigateTo({
                      url: '../../../pages/Visitor/showuser/showuser'
                    })
                  }
                },
                fail: function () {//连接不上服务器，给予提示
                  that.setData({ 'dialog5.hidden': true });
                  that.setData({
                    upload: {
                      hidden: false
                    }
                  });
                }
              })
                      
            }else{//获取不到open，出现异常
              that.setData({
                upload: {
                  hidden: false
                }
              });
                    console.log("出现异常")
                  }
            },  
          fail: function () {
            that.setData({ 'dialog5.hidden': true });
            that.setData({
              upload: {
                hidden: false
              }
            });
            console.log("连接不上服务器")
          },
        })
      }
    })
  }
})